package com.bbs.controller;

import com.bbs.domain.Page;
import com.bbs.entity.Post;
import com.bbs.entity.User;
import com.bbs.service.PostService;
import com.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;

    /**
     * 首页数据封装
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request){
//        postService.findScan();
        // 接收前端提交上来的当前页参数1,2,3.....
        String curPageStr = request.getParameter("curPage");
        int curPage = 1;
        if(null != curPageStr){
            curPage = Integer.parseInt(curPageStr);// 获取前端提交的下一页
        }
        // 查询帖子列表
        Page<Post> pageInfo = postService.findByPage(curPage);
        List<User> hotUserList = userService.listUserByHot();
        List<User> userList = userService.listUserByTime();
        model.addAttribute("pageInfo",pageInfo);//向模型中添加帖子数据数据，给前端页面展示
        model.addAttribute("userList",userList);// 近期加入的用户列表
        model.addAttribute("hotUserList",hotUserList);// 近期活跃的用户列表

        return "index";
    }



    @RequestMapping(value = "/json",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String json(){
        User user = userService.findById(1);
        System.out.println(user);
        return user.toString();
    }
}
